﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Threading;
using PeppeDotNet.SP2010.Html5Fields.Utils;
using System.Globalization;

namespace PeppeDotNet.SP2010.Html5Fields.Number
{
    public class Html5NumberInputField : SPFieldNumber
    {        
        #region Constructors
        public Html5NumberInputField(SPFieldCollection fields, string fieldName) : base(fields, fieldName) { }
        public Html5NumberInputField(SPFieldCollection fields, string typeName, string displayName) : base(fields, typeName, displayName) { } 
        #endregion

        public override BaseFieldControl FieldRenderingControl
        {
            get
            {
                return new BaseHtml5InputFieldControl(Html5InputType.Number) { FieldName = InternalName };
            }
        }

        public double Step
        {
            get 
            {
                double step = 1;
                double.TryParse(FieldsUtility.GetFieldAttribute("Step", GetType(), this), out step);
                return step; 
            }
            set { FieldsUtility.SetFieldAttribute<double>("Step", value, GetType(), this); }
        }

        public override string GetValidatedString(object value)
        {
            double val = 0;
            if(value != null && !double.TryParse(value.ToString(), out val))
                throw new SPFieldValidationException("The value of this field is not a valid number."); //todo: localization!

            return base.GetValidatedString(value);
        }
    }
}
